import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";

const router = createRouter({
  // history: createWebHashHistory(import.meta.env.BASE_URL),
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
  },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/loginv2.vue"),
    },
    
    // {
    //   path: "/loginv2",
    //   name: "loginv2",
    //   component: () => import("@/views/loginv2.vue"),
    // },
    {
      path: "/chat",
      name: "chat",
      component: () => import("@/views/chat.vue"),
    },
    // {
    //   path: "/",
    //   name: "home",
    //   component: () => import("@/views/home.vue"),
    // },
  ],
});
router.beforeEach((to, from, next) => {
  console.log('全局拦截');
  console.log(to);
  // next()

  let token = localStorage.getItem("token") || ""; //在本地存储中获取token
  if (token) {
    //判断是否有token
    next();
  } else {
    //在没有token的前提下，to下面的path是否为/login，如果不是则页面跳转到登录页面
    if (to.path == "/login" || to.path == "/") {
      next();
    } else {
      next({ path: "/login" }); //跳转页面到login页
    }
    next();
  }
})
export default router;
